/*
 * Copyright (C) 2009 Yury Kudryashov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package name.kudryashov.functions;

public class Functions {

    private Functions() {}

    public static <T> Generator<T> constFunction(final T value) {
        return new Generator<T>() {
            public T get() {
                return value;
            }

            public String toString() {
                return "const(" + value + ")";
            }
        };
    }

    public static Function<Object, String> toStringFunction() {
        return ToStringFunction.INSTANCE;
    }

    public static <T> Function<T, T> identityFunction() {
        return new Function<T, T>() {
            public T apply(T arg) {
                return arg;
            }

            public String toString() {
                return "identity";
            }
        };
    }

    private enum ToStringFunction implements Function<Object, String> {
        INSTANCE;

        public String apply(Object arg) {
            return arg.toString();
        }


        public String toString() {
            return "toString";
        }
    }

}
